package com.dm.cloud.oauthlib.conf;


import com.dm.cloud.core.config.CommonConstants;
import com.dm.cloud.core.util.RequsetArrtibutesUtil;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Objects;

/**
 * 全局缓存token
 */
@Component
public class AuthLibFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String token = request.getHeader(CommonConstants.AUTH_HEAD_KEY);
        if(!Objects.isNull(token)) {
            RequsetArrtibutesUtil.put(CommonConstants.AUTH_HEAD_KEY, token);
        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void init(FilterConfig filterConfig){

    }

    @Override
    public void destroy(){

    }
}
